package com.situ.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import com.situ.domain.FruitModel;
import com.situ.domain.SalesRecordModel;

public class SalesRecordService implements Serializable{
    
    /** serialVersionUID*/  
    private static final long serialVersionUID = 1256411L;
    
    ArrayList<SalesRecordModel> salesRecordList = new ArrayList<SalesRecordModel>();
    
    public SalesRecordService() {
        super();
        // 测试数据
//        int sex = 1;
//        String[] names = {"苹果", "香蕉", "西瓜"};
//        double[] sellingPrices = {1.0, 2.0, 3.0};
//        double[] weight = {10, 20, 30};
//        salesRecordList.add(new SalesRecordModel(sex, names, sellingPrices, weight));
    }
    
    /**
     * <p>Title: listSalesRecords</p>  
     * <p>Description: 获取销售记录列表</p>  
     * @author 恩策
     * @date 2021年7月30日
     * @return
     */
    public ArrayList<SalesRecordModel> listSalesRecords() {
        return salesRecordList;
    }
    
    /**
     * <p>Title: addSalesRecord</p>  
     * <p>Description: 新添销售记录</p>  
     * @author 恩策
     * @date 2021年7月30日
     * @param sRecordModel
     */
    public void addSalesRecord(SalesRecordModel sRecordModel) {
        salesRecordList.add(sRecordModel);
    }
    
    /**
     * <p>Title: profit</p>  
     * <p>Description: 获得指定时间内的纯收益</p>  
     * @author 恩策
     * @date 2021年7月30日
     * @param startDate
     * @param endDate
     * @param fruitService
     * @return
     */
    public double profit(Date startDate, Date endDate, FruitService fruitService) {
        double profit = 0.0;
        for (SalesRecordModel salesRecord : salesRecordList) {
            if (salesRecord.getDate().after(startDate) && salesRecord.getDate().before(endDate)) {
                profit += salesRecord.getTotleCost();
                for (int i = 0; i < salesRecord.getName().length; i++) {
                    String fruitName = salesRecord.getName()[i];
                    for (FruitModel fruit : fruitService.listFruit()) {
                        if(fruitName.equals(fruit.getName())) {
                            profit = profit - salesRecord.getWeight()[i] * fruit.getCostPrice();
                            break;
                        }
                    }
                }
            }
        }
        return profit;
    }
    

    
    public SalesRecordModel getLastSalesRecord() {
        return salesRecordList.get(0);
    }
    
    
}
